<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta charset="utf-8">
    <title>教练信息编辑</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="bookmark" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/css/layui.css" media="all">
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/layui/layui.js"
            charset="utf-8"></script>
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/jquery/jquery.js"></script>
    <script type="application/javascript"
            src="${pageContext.request.contextPath}/private/js/loading/ajaxloading.js"></script>
</head>
<body>
<form id="form1" class="layui-form" action="">
    <table width="96%" align="center">
        <tr height="23">
            <td align="center">教练名称:</td>
            <td align="left">
                <input type="hidden" id="coachid" name="coachid" value="${coachInfo.coachid}">
                <input id="coachname" name="coachname" class="layui-input" type="text" placeholder="请输入教练名称" autocomplete="off"
                       lay-verify="coachname" value="${coachInfo.coachname}">
            </td>
        </tr>
        <tr height="23">
            <td align="center">性别:</td>
            <td align="left">
                <input id="sex" name="sex" type="checkbox" lay-skin="switch" value="${coachInfo.sex}" ${coachInfo.sex=="2"?"":"checked"}
                       lay-text="男|女">
            </td>
        </tr>
        <tr height="23">
            <td align="center">教练等级:</td>
            <td>
                <select id="coachlevel" name="coachlevel" lay-verify="coachlevel">
                    <option value="">请选择</option>
                </select>
            </td>
        </tr>
        <tr height="23">
            <td nowrap="nowrap">教练年龄:</td>
            <td nowrap="nowrap">
                <input id="coachage" name="coachage" class="layui-input" type="text" placeholder="请输入教练年龄" autocomplete="off"
                       lay-verify="number" value="${coachInfo.coachage}">
            </td>
        </tr>
        <tr height="23">
            <td nowrap="nowrap">教练联系电话:</td>
            <td nowrap="nowrap">
                <input id="telephone" name="telephone" class="layui-input" type="text" placeholder="请输入教练联系电话" autocomplete="off"
                       lay-verify="telephone" value="${coachInfo.telephone}">
            </td>
        </tr>
        <tr height="23">
            <td nowrap="nowrap">教练入职时间:</td>
            <td nowrap="nowrap">
                <input id="starttime" name="starttime" class="layui-input" type="text" placeholder="yyyy-MM-dd"
                       autocomplete="off" value="${coachInfo.starttime_str}"
                       lay-verify="starttime">
            </td>
        </tr>
        <tr height="23">
            <td nowrap="nowrap">教练离职时间:</td>
            <td nowrap="nowrap">
                <input id="endtime" name="endtime" class="layui-input" type="text" placeholder="yyyy-MM-dd"
                       autocomplete="off" value="${coachInfo.endtime_str}"
                       lay-verify="endtime">
            </td>
        </tr>

        <tr height="50">
            <td align="center">
                <button id="formsubmit" class="layui-btn" lay-submit lay-filter="savecoachinfo">保存</button>
            </td>
        </tr>
    </table>
</form>
</body>
<script type="application/javascript">
    var form;
    layui.use(['form','laydate'], function(){
        form = layui.form;
        //日期控件
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#starttime' //指定元素
        });
        laydate.render({
            elem: '#endtime' //指定元素
        });
        //switch off状态赋值
        form.on('switch', function (data) {
            if(data.elem.id=='validflag')
                $(data.elem).attr('type', 'hidden').val(this.checked ? '1' : '0');
            if(data.elem.id=='sex')
                $(data.elem).attr('type', 'hidden').val(this.checked ? '1' : '2');
        });
        //表单验证规则自定义
        form.verify({
            coachname: function(value){
                if(!value) {
                    return '教练姓名不能为空';
                }
            },
            coachlevel: function(value){
                if(!value) {
                    return '教练等级不能为空';
                }
            },
            telephone: function(value){
                if(!value&&value.length>11) {
                    return '联系电话不能超过11位';
                }
            }
        });

        //监听提交
        form.on('submit(savecoachinfo)', function(data){
            $.ajax({
                data : data.field,
                type : "POST",
                url : "${pageContext.request.contextPath}/eyas/coachinfo/saveCoachInfo",
                dataType : "json",
                success: function(data) {
                    layer.msg("保存成功,页面将在1秒后关闭");
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },1000);
                },
                error:function(xdata){
                    layer.msg(xdata.responseJSON.errormsg);
                }
            });
            return false;
        });
        selectRoleName();

    });


    function selectRoleName(){
        $.ajax({
            url:"${pageContext.request.contextPath}/eyas/admin/dictionary/COACHLV",
            type:"GET",
            dataType:"json",
            success:function(result){
                var list = result;    //返回的数据
                var role = document.getElementById("coachlevel");        //add_role_name给select定义的id
                for(var i=0;i<list.length;i++){
                    var option = document.createElement("option");    // 创建添加option属性
                    option.setAttribute("value",list[i].keycode);                  // 给option的value添加值
                    if(list[i].keycode=='${coachInfo.coachlevel}')
                        option.setAttribute("selected","selected");
                    option.innerText=list[i].keyworlds;             // 打印option对应的纯文本 （超级管理员、管理员）
                    role.appendChild(option);                          // 给select 添加option子标签
                }

                form.render("select");                                // 刷性select，显示出数据
            },
            error:function(xdata){
                layer.msg(xdata.responseJSON.errormsg);
            }
        });
    }
</script>
</html>
